package com.lijie.ljblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.ljblog.common.BlogConstants;
import com.lijie.ljblog.common.GameAbstractStrategy;
import com.lijie.ljblog.mapper.BlogMapper;
import com.lijie.ljblog.mapper.GameMapper;
import com.lijie.ljblog.mapper.GameScoreMapper;
import com.lijie.ljblog.model.Blog;
import com.lijie.ljblog.model.Game;
import com.lijie.ljblog.model.GameScore;
import com.lijie.ljblog.model.dto.GameScoreDTO;
import com.lijie.ljblog.model.dto.GameScoreSaveDTO;
import com.lijie.ljblog.model.vo.GameScoreRankVO;
import com.lijie.ljblog.model.vo.GameScoreVO;
import com.lijie.ljblog.service.BlogService;
import com.lijie.ljblog.service.GameService;
import com.lijie.ljblog.util.QueryWrapperUtil;
import com.lijie.ljblog.util.RedisUtils;
import com.lijie.ljblog.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@Service
public class GameServiceImpl extends GameAbstractStrategy implements GameService {

    @Resource
    GameMapper gameMapper;
    @Resource
    GameScoreMapper gameScoreMapper;

    public GameServiceImpl() {
        register(BlogConstants.GAME_TYPE_SAOLEI);
        register(BlogConstants.GAME_TYPE_CAT);
        register(BlogConstants.GAME_TYPE_TETTRIES);
    }
    /**
     * 根据人员和游戏名称获取得分
     *
     * @param gameScoreDTO
     * @return
     */
    @Override
    public GameScoreVO getGameScore(GameScoreDTO gameScoreDTO) {
        return gameMapper.getGameScore(gameScoreDTO);
    }

    /**
     * 保存游戏通过时间
     *
     * @param gameScoreSaveDTO
     * @return
     */
    @Override
    public Boolean saveGameScore(GameScoreSaveDTO gameScoreSaveDTO) {
        GameScore gameScore = new GameScore();
        BeanUtils.copyProperties(gameScoreSaveDTO, gameScore);
        List<Game> games = gameMapper.selectList(new LambdaQueryWrapper<Game>().eq(Game::getGameName, gameScoreSaveDTO.getGameName()));
        gameScore.setGameId(games.get(0).getGameId());
        int insert = gameScoreMapper.insert(gameScore);
        return insert>0;
    }

    /**
     * 获得游戏得分排名
     *
     * @param gameScoreDTO
     * @return
     */
    @Override
    public List<GameScoreRankVO> getGameScoreRank(GameScoreDTO gameScoreDTO) {
        return gameScoreMapper.getGameScoreRank(gameScoreDTO);
    }

}
